package io.yunho.yunho.interview.two.lock;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: Jason
 * @date: 2021/3/18 15:44
 * @description:
 *  手写一个自旋锁
 *  好处：使用自旋锁，不需要类似wait的阻塞。
 *
 *  设计：通过CAS操作完成自旋锁，A线程先进来调用mylock方法自己持有锁5秒钟，
 *  B随后进来发现当前线程持有锁，所以只能通过自旋等待，直到A释放锁后B随后抢到。
 *
 */
public class SpinlockDemo {
    AtomicReference<Thread> atomicReference = new AtomicReference<>();
    public void runSout(Thread thread1,Thread thread2){
        String threadName = thread1 !=null?thread1.getName():"空的";
        System.out.println("进入runSout\t"+Thread.currentThread().getName()+"\t thread1 \t"+threadName+" 当前值:\t"+atomicReference.get());
        while (!atomicReference.compareAndSet(thread1,thread2)){
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println("自旋中**********************"+"当前线程\t"+Thread.currentThread().getName());
        }
        System.out.println("完成runSout\t"+Thread.currentThread().getName()+"\t thread1 \t"+threadName+" 当前值:\t"+atomicReference.get());
    }

    /**
     * 利用自旋锁实现 lock
     */
    public void myLock(){
        while (!atomicReference.compareAndSet(null,Thread.currentThread())){

        }
        System.out.println("my lock \t "+Thread.currentThread().getName());
    }
    /**
     * 利用自旋锁实现 lock
     */
    public void myunLock(){
        atomicReference.compareAndSet(Thread.currentThread(),null);
        System.out.println("my unlock \t "+Thread.currentThread().getName());
    }

    public static void main(String[] args) {
//        SpinlockDemo spinlockDemo = new SpinlockDemo();
//        Thread threadA = new Thread(() -> {
//            try {
//                //停顿5秒 延迟AA线程进入自旋锁对应方法
//                TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
//            spinlockDemo.runSout(null, Thread.currentThread());
//        }, "AA");
//        threadA.start();
//        //停顿一秒 确保AA线程先开启
//        try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
//
//        Thread threadB = new Thread(() -> {
//            spinlockDemo.runSout(threadA, Thread.currentThread());
//        }, "BB");
//        threadB.start();
        //测试自定义锁
        SpinlockDemo spinlockDemo = new SpinlockDemo();
        Thread threadA = new Thread(() -> {
            spinlockDemo.myLock();
            try {
                //停顿5秒 延迟AA线程进入自旋锁对应方法
                TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
            spinlockDemo.myunLock();
        }, "AA");
        threadA.start();
        //停顿一秒 确保AA线程先开启
        try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}

        Thread threadB = new Thread(() -> {
            spinlockDemo.myLock();
            try {
                //停顿5秒 延迟AA线程进入自旋锁对应方法
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {e.printStackTrace();}
            spinlockDemo.myunLock();
        }, "BB");
        threadB.start();
    }
}
